package de.cau.apo.problems.strippacking;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import de.cau.apo.problems.IInstance;
import de.cau.apo.utils.InputStreamParser;

/**
 * This class represents an instance of the strip packing problem.
 * There is on the one hand an array of strips with different dimensions that has 
 * to be packed and on the other hand there is a maximum strip width
 * that must not be exceeded while packing.
 * 
 * @author Marius Richter
 *
 */
public class SPInstance implements IInstance {

	// ########### properties ###########
    
	/** the maximum strip width that must not be exceeded while packing */
    public float maxStripWidth = 0;
    /** the array of rectangles that need to be packed */
    public Rectangle[] rectangles = null;
    
    // ########### instantiation ###########
    
    /**
     * Default empty constructor
     */
    public SPInstance() {
    }
    
    // ########### getter/setter ###########
    
    /**
     * @return the maximum strip width
     */
    public float getMaxStripWidth() {
		return maxStripWidth;
	}
    
    /**
     * @return the array of rectangles that need to be packed
     */
    public Rectangle[] getRectangles() {
		return rectangles;
	}
    
    /**
     * @param index the index of a rectangle
     * @return the rectangle that has the given index
     */
    public Rectangle getRectangle(int index) {
    	Rectangle strip = this.rectangles[index];
    	if (strip.getIndex() != index) {
    		for (Rectangle rect : this.rectangles) {
    			if (rect.getIndex() == index) {
    				return rect;
    			}
    		}
    	}
    	return strip;
    }
    
    /**
     * @return the number of rectangles that need to be packed
     */
    public int getNumberRectangles() {
        if (this.rectangles == null) {
            return 0;
        }
        return this.rectangles.length;
    }
    
	// ########### IInstance implementation ###########
	
    /**
	 * {@inheritDoc}
	 */
	@Override
	public boolean parseInstance(String instancePath) throws FileNotFoundException {
		FileInputStream in = new FileInputStream(instancePath);
        
        try {
        	//the first number is the maximum strip width
            this.maxStripWidth = InputStreamParser.readNextFloat(in);
            if (this.maxStripWidth == -1) {
                this.maxStripWidth = 0;
                return false;
            }
            
            //the second number is the number of rectangles following this
            int numberRectangles = InputStreamParser.readNextInt(in);
            if (numberRectangles == -1) {
                this.maxStripWidth = 0;
                return false;
            }
            this.rectangles = new Rectangle[numberRectangles];
            
            //read the rectangle sizes
            for (int i = 0;i < numberRectangles;i++) {
                float width = InputStreamParser.readNextFloat(in);
                if (width == -1) {
                    this.maxStripWidth = 0;
                    this.rectangles = null;
                    return false;
                }
                float height = InputStreamParser.readNextFloat(in);
                if (width == -1) {
                    this.maxStripWidth = 0;
                    this.rectangles = null;
                    return false;
                }
                this.rectangles[i] = new Rectangle(i, width, height);
            }
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
            return false;
        }
        return true;
	}

}
